﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;

namespace Prodma.DataAccess.DataService
{
    public class UnitService : IUnitRepository
    {

        private DataContext context = new DataContext();


        #region IUnitRepository Members

        public IList<int> GetParentUnitList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from unit in context.tb_Unit
                            group unit by unit.ParentId into parent
                            select parent.Key).ToList();

                return list;
            }
        }
        #endregion

        #region IRecordOperation<tb_Unit,UnitVm> Members

        public IList<UnitVm> GetList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from unit in context.tb_Unit
                            select new UnitVm()
                            {
                                Id = unit.Id,
                                Name = unit.Name,
                                Parent = new ParentUnitVm() { Id = unit.ParentId },
                                State = new StateVm() { Id = unit.tb_State.Id, Name = unit.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        public void New(tb_Unit entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Unit(entity);
                context.SaveChanges();
            }
        }
        public UnitVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from unit in context.tb_Unit
                            where unit.Id == id
                            select new UnitVm()
                            {
                                Id = unit.Id,
                                Name = unit.Name,
                                Parent = new ParentUnitVm() { Id = unit.ParentId },
                                State = new StateVm() { Id = unit.tb_State.Id }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Unit entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from unit in context.tb_Unit
                            where unit.Id == id
                            select unit).FirstOrDefault();

                edit.Name = entity.Name;
                edit.ParentId = entity.ParentId;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from unit in context.tb_Unit
                              where unit.Id == id
                              select unit).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IFindOperation<UnitVm> Members

        public IList<UnitVm> GetList(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from unit in context.tb_Unit
                            where unit.Name.StartsWith(findName)
                            select new UnitVm()
                            {
                                Id = unit.Id,
                                Name = unit.Name,
                                Parent = new ParentUnitVm() { Id = unit.ParentId },
                                State = new StateVm() { Id = unit.tb_State.Id, Name = unit.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        #endregion

        #region IRowCountOperation Members

        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var rowCount = (from unit in context.tb_Unit
                                select unit).Count();

                return rowCount;
            }
        }
        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var rowCount = (from unit in context.tb_Unit
                                where unit.Name.StartsWith(findName)
                                select unit).Count();

                return rowCount;
            }
        }
        #endregion

        #region IParentRelationshipOperation Members

        public IList<int> GetCounts()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var countList = (from unit in context.tb_Unit
                                 group unit by unit.ParentId into parent
                                 select parent.Count()).ToList();

                return countList;
            }
        }
        public IList<int> GetCounts(string find)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var countList = (from unit in context.tb_Unit
                                 where unit.Name.StartsWith(find)
                                 group unit by unit.ParentId into parent
                                 select parent.Count()).ToList();

                return countList;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from unit in context.tb_Unit
                             where unit.Id == id
                             select unit).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from unit in context.tb_Unit
                             where unit.Id == id
                             select unit).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

        
    }
}
